<template>
  <div>
    <div class="app-container">
      <el-card>
        <el-row
          type="flex"
          align="middle"
          class="row-title"
        >
          <span>区域搜索:</span> <el-input
            v-model="nodeList.name"
            placeholder="请输入"
            style="width: 200px;"
          />
          <el-button class="search-btn" type="primary" icon="el-icon-search" @click="searchFn">查询</el-button>
        </el-row>
      </el-card>
      <el-card class="card-main">
        <el-button class="addNew" icon="el-icon-plus" @click="addNewTask">新建</el-button>
        <el-table
          :data="tableData"
          style="width: 100%"
        >
          <el-table-column
            type="index"
            :index="tableIndex"
            label="序号"
          />
          <el-table-column
            prop="name"
            label="区域名称"
          />
          <el-table-column
            prop="nodeCount"
            label="点位数"
          />
          <el-table-column
            prop="remark"
            label="备注说明"
          />
          <el-table-column
            label="操作"
          >
            <!-- <template #default="{data}"> -->
            <template #default="scoped">
              <el-button type="text" @click="checked(scoped.row)">查看详情</el-button>
              <el-button type="text" @click="checkNode(scoped.row)">修改</el-button>
              <el-button type="text" style="color:red" @click="delInfo(scoped.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-row>
          <el-pagination
            v-if="totalCount>10"
            layout="slot"
          >
            <el-row type="flex" justify="space-between" align="middle" style="height:60px">
              <el-col :span="6" style="color:#ccc;"><span style="margin-left:5px; font-size:16px;font-weight:200">共{{ totalCount }}条记录 </span><span style="margin-left:5px;font-size:16px;font-weight:200">第{{ nodeList.pageIndex+'/'+totalPage }}页</span></el-col>
              <!-- <el-col :span="6" />
          <el-col :span="6" /> -->
              <el-col :span="6">
                <el-button class="btnStyle" :disabled="upBtnStatus" @click="upBtn">上一页</el-button>
                <el-button class="btnStyle" :disabled="nextBtnStatus" @click="nextBtn">下一页</el-button>
              </el-col>
            </el-row>
          </el-pagination>
        </el-row>
      </el-card>
    </div>
    <!-- 新建工单 会话框 -->
    <!-- <Dialog title="新增工单" :new-tack-show="newTackShow">
      <el-form>
        11
      </el-form>
    </Dialog> -->
    <el-dialog
      :title="title"
      :visible="newTaskShow"
      width="50%"
      :close-on-click-modal="false"
      custom-class="dialog-style"
      @close="close"
    >
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" style=" font-weight: 200;">
        <el-form-item label="区域名称" prop="regionName">
          <el-input v-model="ruleForm.regionName" show-word-limit maxlength="15" style="width:80%" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="备注说明" prop="remark">
          <el-input v-model="ruleForm.remark" show-word-limit maxlength="50" style="width:80%" placeholder="请输入备注（不超过40字）" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 区域详情 -->
    <el-dialog
      title="区域详情"
      :visible="itemNodeShow"
      width="50%"
      :close-on-click-modal="false"
      custom-class="dialog-style"
      @close="itemNodeShow=false"
    >
      <el-form ref="form" label-width="100px">
        <p><span>区域名称： </span> {{ checkItem.name }}</p>
        <el-form-item label="包含点位：">
          <el-table
            :data="infoItemNode"
            style="width: 80%"
          >
            <el-table-column
              type="index"
              label="序号"
            />
            <el-table-column
              prop="name"
              label="点位名称"
            />
            <el-table-column
              prop="vmCount"
              label="设备数量"
            />
          </el-table>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { nodeSearchApi, addNodeApi, putNodeApi, infoNodeApi, nodeSiteApi, delNodeApi } from '@/api'

export default {

  data() {
    return {
      infoItemNode: [],

      itemNodeShow: false, // 区域详情显示隐藏
      checkItem: {}, // 区域详情
      // 获取所有的数据 更新 或者 搜索 参数
      nodeList: {
        pageIndex: 1, // 请求的页 当前页
        pageSize: 10, // 一页十条数据
        name: ''
      },
      totalCount: 0, // 一共的条数
      totalPage: 0, // 总页数
      tableData: [], // 表单数据
      upBtnStatus: false, // 按钮是否可以点击的状态
      nextBtnStatus: false,
      newTaskShow: false, // 新建工单  弹出层 显示隐藏

      ruleForm: {
        regionName: '', // 区域名称
        remark: '' // 备注说明
      },
      rules: {
        regionName: [
          { required: true, message: '请输入' },
          { max: 15, message: '长度在 不超过 15 个字符' }
        ],
        remark: [
          { required: true, message: '请输入' },
          { max: 50, message: '长度在 不超过 50 个字符' }
        ]
      }
    }
  },
  computed: {
    title() {
      return this.ruleForm.id ? '修改区域' : '新增区域'
    }
  },
  created() {
    this.fetchNodeFn()
  },
  methods: {
    searchFn() {
      // 区域查询
      this.fetchNodeFn()
    },
    // 删除区域
    async delInfo(val) { // 删不了
      console.log(val)
      this.$message.success('无法删除')
      const res = await delNodeApi(val.id)
      console.log(res)
      // this.$message.success('删除区域成功')
      // this.fetchNodeFn()
    },
    // 查看详情
    async checked(val) {
      this.itemNodeShow = true
      const { data } = await infoNodeApi(val.id) // 根据id查 详情
      console.log(data)
      this.checkItem = data
      // 获取  包含点位请求
      const { data: data1 } = await nodeSiteApi({ ...this.nodeList, regionId: data.id })
      console.log(data1)
      this.infoItemNode = data1.currentPageRecords
    },
    // 修改区域
    checkNode(val) {
      console.log(val)

      this.ruleForm = {
        regionName: val.name, // 区域名称
        remark: val.remark, // 备注说明
        id: val.id
      }
      // this.itemNode = val
      this.newTaskShow = true
    },
    // 关闭新增区域 弹出层的 close
    close() {
      this.newTaskShow = false
      // 清空表单
      this.$refs.ruleForm.resetFields()
      this.ruleForm = {
        regionName: '', // 区域名称
        remark: '', // 备注说明
        id: ''
      }
    },
    // 提交新建的工单
    submit() {
      this.$refs.ruleForm.validate(async(valid) => {
        if (!valid) return
        if (this.ruleForm.id) {
          await putNodeApi(this.ruleForm)
        } else {
          await addNodeApi(this.ruleForm)
        }
        this.$message.success(this.ruleForm.id ? '修改区域成功' : '新增区域成功')
        this.fetchNodeFn()
        this.close()
      })
    },

    // 新建
    addNewTask() {
      this.newTaskShow = true
    },

    // 分页里的 下一页上一页
    upBtn() {
      if (this.nodeList.pageIndex === 1) {
        this.upBtnStatus = true // 按钮是否可以点击的状态
        this.$message('当前为第一页')
        return
      }
      this.nextBtnStatus = false
      this.nodeList.pageIndex--
      this.fetchNodeFn()
    },
    nextBtn() {
      if (this.nodeList.pageIndex === this.totalPage) {
        this.nextBtnStatus = true
        this.$message('当前为第最后一页')
        return
      }
      this.upBtnStatus = false
      this.nodeList.pageIndex++
      this.fetchNodeFn()
    },
    // 处理时间的函数`
    // handleDate(value) {
    //   return dayjs(value).format('YYYY-MM-DD HH:mm:ss')
    // },
    // 获取所有的数据 更新 或者 搜索
    async fetchNodeFn() {
      const { data } = await nodeSearchApi(this.nodeList)
      console.log(data)
      this.tableData = data.currentPageRecords
      this.totalCount = +data.totalCount // 总数
      this.totalPage = +data.totalPage // 总页数
    },
    // 分页 index 累加
    tableIndex(index) {
      return (this.nodeList.pageIndex - 1) * this.nodeList.pageSize + index + 1
    }
  }
}
</script>

<style >
  .dialog-style{
    border-radius: 8px;
  }
</style>
<style lang='scss' scoped>
.app-container {
  padding:20px;
  .row-title{
  span {
    font-size: 14px;
    margin-right:10px;
    margin-left:10px;
    }
  }
  .search-btn{
    margin-left: 10px;
  }
  .card-main {
    margin-top: 20px;
    // color: #fff;
    .addNew{
      color: #fff;
      // font-size: 16px;
     background: linear-gradient(135deg,#ff9743,#ff5e20)!important;
    border: none;
    }
  }
  .btnStyle{
    background-color: #d5ddf8;
    width: 70px;
    height: 32px;
    margin: 0 16px;
    border-radius: 2px;

  }
}
</style>
